package cz.schovjan.proj.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import cz.schovjan.proj.dao.IBandDao;
import cz.schovjan.proj.entity.BandEntity;

/**
 * Implementace IBandDao.
 * @author schovjan
 * 
 */
@Repository
public class JpaBandDao implements IBandDao {

	private static final String FIND_ALL = "select b from BandEntity as b";
	
	@PersistenceContext
	private EntityManager em;
	
	
	@Override
	@SuppressWarnings("unchecked")
	public List<BandEntity> findAll() {
		return em.createQuery(FIND_ALL).getResultList();
	}
	
	
	@Override
	public void add(BandEntity be) {
		em.persist(be);
	}
	
	
	@Override
	public BandEntity find(Integer id) {
		return em.find(BandEntity.class, id);
	}
	
	
	@Override
	public void delete(Integer id) {
		BandEntity be = find(id);
		if (be != null) {
			em.remove(be);
		}
	}
	
	
	@Override
	public void save(BandEntity be) {
		em.merge(be);
	}
	
}
